package com.forever7776.life.core.service.impl.praise;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.forever7776.life.core.entity.praise.PraiseDetailBlog;
import com.forever7776.life.core.mapper.praise.PraiseDetailBlogMapper;
import com.forever7776.life.core.service.praise.IPraiseDetailBlogService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 博客点赞详细表 服务实现类
 * </p>
 *
 * @author kz
 * @since 2018-04-11
 */
@Service
public class PraiseDetailBlogServiceImpl extends ServiceImpl<PraiseDetailBlogMapper, PraiseDetailBlog> implements IPraiseDetailBlogService {

    @Autowired
    private PraiseDetailBlogMapper praiseDetailBlogMapper;

    /**
     * 查询一条点赞状态的博客点赞详细数据
     *
     * @param userId
     * @param blogId
     * @return
     */
    @Override
    public PraiseDetailBlog selectPraiseOneByUserIdAndBlogId(String userId, Integer blogId) {
        return selectOne(new EntityWrapper<PraiseDetailBlog>().
                eq("user_id", userId).eq("blog_id", blogId).eq("status", 0));
    }

    /**
     * 查询一条未点赞状态的博客点赞详细数据
     *
     * @param userId
     * @param blogId
     * @return
     */
    @Override
    public PraiseDetailBlog selectNotPraiseOneByUserIdAndBlogId(String userId, Integer blogId) {
        return selectOne(new EntityWrapper<PraiseDetailBlog>().
                eq("user_id", userId).eq("blog_id", blogId).eq("status", 1));
    }

    /**
     * 查询一条数据，如果没有则插入
     *
     * @param userId
     * @param blogId
     * @return
     */
    @Override
    public PraiseDetailBlog selectOneOrInsert(String userId, Integer blogId) {
        PraiseDetailBlog detail = praiseDetailBlogMapper.selectOneByUserIdAndBlogId(userId, blogId);
        if (detail == null) {
            detail = new PraiseDetailBlog();
            detail.setUserId(userId);
            detail.setBlogId(blogId);
            praiseDetailBlogMapper.insert(detail);
        }
        return detail;
    }
}
